package org.petstore.web.servlet;

import org.petstore.domain.Account;
import org.petstore.domain.Order;
import org.petstore.service.OrderService;
import org.petstore.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

import static org.petstore.web.servlet.util.ServletUtil.LOGIN_FORM;

public class ViewOrderServlet extends HttpServlet {
    private static final String VIEW_ORDER = "/WEB-INF/jsp/order/ListOrder.jsp";
    private static final String CONFIRM_ORDER = "/WEB-INF/jsp/order/ConfirmOrder.jsp";
    private static final String LIST_ORDERS = "/WEB-INF/jsp/order/ListOrders.jsp";
    private static final String NEW_ORDER = "/WEB-INF/jsp/order/NewOrderForm.jsp";
    private static final String SHIPPING = "/WEB-INF/jsp/order/ShippingForm.jsp";

    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute("loginUser");
        if (account == null) {
            request.setAttribute("msg", "请先登录");
            request.getRequestDispatcher(LOGIN_FORM).forward(request, response);

        }
        OrderService orderService = new OrderServiceImpl();
        List<Order> orderList = orderService.getOrdersByUsername(account.getUsername());
        if (orderList == null) {
            request.setAttribute("message", "订单为空");
            request.getRequestDispatcher(LIST_ORDERS).forward(request, response);
        } else {
            session.setAttribute("orderList", orderList);
            request.getRequestDispatcher(LIST_ORDERS).forward(request, response);

        }

    }
}
